<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="referrer" content="no-referrer" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客正文</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_content.css">
    <link rel="stylesheet" href="editor.md/css/editormd.preview.min.css" />
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="js/urlutils.js"></script>
</head>

<body> 
    <!-- 导航栏 -->
    <div class="nav">
        <img src="./img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="myblog_list.html">我的主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="login.html">登陆</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <!-- img/avatar.png -->
                <img id="photo" referrer="no-referrer|origin|unsafe-url" src="./img/avatar1.png" class="avtar" onerror="this.src='./img/avatar1.png'">
                <h3 id = "username"></h3>
                <a href="https://gitee.com/dashboard">gitee 地址</a>
                <div class="counter">
                    <span>文章</span>
                </div>
                <div class="counter">
                    <span id="artcount"></span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right">
            <div class="blog-content">
                <!-- 博客标题 -->
                <h3 id="title"></h3>
                <!-- 博客时间 -->
                <div class="date">
                    发布时间：<span id="createtime"></span>
                    阅读量：<span id="rcount"></span>
                </div>
                <!-- 博客正文 -->
                <div id="editorDiv">

                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
            var aid = getParamValue("aid");
            var editormd;
            function initEdit(md){
                editormd = editormd.markdownToHTML("editorDiv", {
                markdown : md, // Also, you can dynamic set Markdown text
                // htmlDecode : true,  // Enable / disable HTML tag encode.
                // htmlDecode : "style,script,iframe",  // Note: If enabled, you should filter some dangerous HTML tags for website security.
                });
            }
            // 初始化页面
            function init(){
                // 1. 校验参数
                if (aid == null || aid < 0){
                    alert("参数有误");
                    return false;
                }
                // 2. 请求后端获取数据
                jQuery.ajax({
                    url:"/art/detail",
                    type:"GET",
                    data:{
                        "aid":aid
                    },
                    success:function(res){
                        // 3. 将数据展示到前端
                        if (res.code == 200 && res.data != null){
                            var user = res.data.user;
                            var art = res.data.art;
                            if (user != null){
                                var userId = user.id;
                                console.dir(user)
                                // 给用户对象设置值
                                // jQuery("#photo").attr("onerror", "./img/avatar1.png");
                                jQuery("#photo").attr("src", "./img/photo/"+ userId +".jpg");
                                jQuery("#username").html(user.username);
                                jQuery("#artcount").html(user.artCount); // 用户发布的总文章数
                                // initEdit();
                            }else{
                                alert("查询失败1" + res.msg);
                            }
                            if (art != null){
                                jQuery("#title").html(art.title);
                                jQuery("#createtime").html(art.createtime);
                                jQuery("#rcount").html(art.rcount); // 阅读量
                                initEdit(art.content);
                            }else{
                                alert("查询失败2" + res.msg);
                            }
                        }else{
                            alert("查询失败3" + res.msg);
                        }
                    }
                });
            }
            init();

            // 访问量
            function incrementRCount(){
                if (aid == null || aid <= 0){
                    return false;
                }
                jQuery.ajax({
                    url:"/art/increment_rcont",
                    type:"POST",
                    data:{
                        "aid":aid
                    },
                    success:function(res){
                        
                    }
                });
            }
            incrementRCount();

            // function checkImgExists(imgUrl){
            //     return new Promise(function(resolve, reject){
            //         var ImgObj = new Image();
            //         ImgObj.src = imgUrl;
            //         ImgObj.onload = function(res){
            //             resolve(res);
            //         };
            //     ImgObj.onerror = function(err){
            //         reject(err);
            //     };
            //     })
            // }
    </script> 
</body>

</html>